package site.marqstree.latte.ec.main.index;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

import site.marqstree.latte.ui.recycler.DataConverter;
import site.marqstree.latte.ui.recycler.ItemType;
import site.marqstree.latte.ui.recycler.MultipleFields;
import site.marqstree.latte.ui.recycler.MultipleItemEntity;

/*
 * 项目名: EleCommerce
 * 包名: site.marqstree.latte.ec.main.index
 * 文件名: IndexDataConverter
 * 创建者: marqstree
 * 创建时间: 2020/1/23 9:12
 * 描述: 首页列表的json转View Model数组
 * json格式：
 * "data": [
      {
        "goodsId": 0,
        "spanSize": 4,
        "banners": [
          "http://ww1.sinaimg.cn/large/0065oQSqly1g2pquqlp0nj30n00yiq8u.jpg",
          "https://ww1.sinaimg.cn/large/0065oQSqly1g2hekfwnd7j30sg0x4djy.jpg",
          "https://ws1.sinaimg.cn/large/0065oQSqgy1fwyf0wr8hhj30ie0nhq6p.jpg"
        ]
      },
      {
        "goodsId": 1,
        "text": "测试描述1",
        "imageUrl": "https://ws1.sinaimg.cn/large/0065oQSqgy1fy58bi1wlgj30sg10hguu.jpg",
        "spanSize": 4
      },
      {
        "goodsId": 2,
        "text": "测试描述2",
        "imageUrl": "https://ws1.sinaimg.cn/large/0065oQSqgy1fxno2dvxusj30sf10nqcm.jpg",
        "spanSize": 2
      },
      {
        "goodsId": 3,
        "text": "测试描述3",
        "imageUrl": "https://ws1.sinaimg.cn/large/0065oQSqgy1fxd7vcz86nj30qo0ybqc1.jpg",
        "spanSize": 2
      }
    ]
 */
public final class IndexDataConverter extends DataConverter {

    //json字符串转View Model数组函数
    @Override
    public ArrayList<MultipleItemEntity> convert() {

        //清空列表
        ArrayList<MultipleItemEntity> list = new ArrayList<>();

        final JSONArray dataArray = JSON.parseObject(getJsonData()).getJSONArray("data");
        final int size = dataArray==null?0:dataArray.size();

        if(size>0) {
            for (int i = 0; i < size; i++) {
                final JSONObject data = dataArray.getJSONObject(i);

                final String imageUrl = data.getString("imageUrl");
                final String text = data.getString("text");
                final int spanSize = data.getInteger("spanSize");
                final int id = data.getInteger("goodsId");
                final JSONArray banners = data.getJSONArray("banners");

                final ArrayList<String> bannerImages = new ArrayList<>();
                int type = 0;
                if (imageUrl == null && text != null) {
                    type = ItemType.TEXT;
                } else if (imageUrl != null && text == null) {
                    type = ItemType.IMAGE;
                } else if (imageUrl != null) {
                    type = ItemType.TEXT_IMAGE;
                } else if (banners != null) {
                    type = ItemType.BANNER;
                    //轮播图url
                    final int bannerSize = banners.size();
                    for (int j = 0; j < bannerSize; j++) {
                        final String banner = banners.getString(j);
                        bannerImages.add(banner);
                    }
                }

                final MultipleItemEntity entity = MultipleItemEntity.builder()
                        .setField(MultipleFields.ITEM_TYPE, type)
                        .setField(MultipleFields.SPAN_SIZE, spanSize)
                        .setField(MultipleFields.ID, id)
                        .setField(MultipleFields.TEXT, text)
                        .setField(MultipleFields.IMAGE_URL, imageUrl)
                        .setField(MultipleFields.BANNERS, bannerImages)
                        .build();

                //写入View Model数组
                list.add(entity);

            }
        }

        //返回View Model数组
        return list;
    }
}
